package com.xnchat.pro.service;

import com.xnchat.pro.enums.MESSAGETYPE;
import com.xnchat.pro.vo.MessageVO;
import com.xnchat.pro.vo.TalkMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * @author 小凝
 */
@Service
public class HandlerService {

    @Autowired
    MessageService messageService;
    @Autowired
    ClockService clockService;
    @Autowired
    RobotHandler robotHandler;
    public void saveMessage(MessageVO messageVO,String code){
        List<TalkMessage> talks=messageVO.getTalks();
        long ct = System.currentTimeMillis()/1000L-120;

        try{
            if(talks!=null&&talks.size()>0) {
                talks.parallelStream().forEach(m->{
                   String tm= m.getTime();
                    String[] split = tm.split("\\.");
                    tm=split[0];
                    Long msgTime=Long.parseLong(tm);
                    if(MESSAGETYPE.ME.getValue().equals(m.getType())){
                        if(msgTime>ct)
                            robotHandler.huiDa(m.getFrom(),m.getContent(),code);
                        messageService.savemessage(m.getFrom().getName(),m.getFrom().getTripcode(),
                                m.getContent(),m.getTime(),m.getType(),messageVO);
                    }
                    else if(MESSAGETYPE.MESSAGE.getValue().equals(m.getType())){
                        if(msgTime>ct)
                        robotHandler.MingLing(m.getFrom(),m,code);
                        messageService.saveByUrl(m.getFrom().getName(),m.getFrom().getTripcode(),
                                m.getMessage(), m.getTime(),m.getUrl(),m.getType(),messageVO);
                    }
                    else if(MESSAGETYPE.LEAVE.getValue().equals(m.getType())){
                        messageService.savemessage("Admin:",m.getUser().getTripcode(),
                                m.getUser().getName()+"离开房间",m.getTime(),m.getType(),messageVO);
                    }
                    else if(MESSAGETYPE.JOIN.getValue().equals(m.getType())){
                        if(msgTime>ct)
                            robotHandler.welcome(m.getUser(),code);
                        messageService.savemessage("Admin:",m.getUser().getTripcode(),
                                m.getUser().getName()+"加入房间",m.getTime(),m.getType(),messageVO);
                    }
                    else if(MESSAGETYPE.KICK.getValue().equals(m.getType())){
                        messageService.savemessage("Admin:",m.getTo().getTripcode(),
                                m.getTo().getName()+"连接中断",m.getTime(),m.getType(),messageVO);
                    }
                    else if(MESSAGETYPE.BAN.getValue().equals(m.getType())){
                        messageService.savemessage("Admin:",m.getTo().getTripcode(),
                                m.getTo().getName()+"禁止进入房间",m.getTime(),m.getType(),messageVO);
                    }
                    else if(MESSAGETYPE.MUSIC.getValue().equals(m.getType())){
                        messageService.saveByUrl("Admin:",
                                m.getFrom().getTripcode(),
                                m.getFrom().getName()+"分享了音乐，名字是："+m.getMusic().getName(),
                                m.getTime(),m.getMusic().getShareURL(),MESSAGETYPE.MUSIC.getValue(),messageVO);
                    }

                });
            }
        }catch(Exception e){
            System.out.println("null"+talks+e);
        }
    }
}
